﻿using System;
using System.IO.Ports;
using System.Windows.Forms;

namespace SerialDebugger
{
    public partial class Form1 : Form
    {
        private SerialPort serialPortSend; // 用于发送数据的串口 (COM1)
        private SerialPort serialPortReceive; // 用于接收数据的串口 (COM2)

        public Form1()
        {
            InitializeComponent();
            InitializeSerialPorts();
        }

        // 初始化串口
        private void InitializeSerialPorts()
        {
            // 初始化发送串口 (COM1)
            serialPortSend = new SerialPort("COM1", 115200, Parity.None, 8, StopBits.One);
            serialPortSend.Open(); // 打开串口

        }

        // 数据接收
        private void SerialPortReceive_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            string data = serialPortReceive.ReadLine(); // 读取串口数据
            this.Invoke(new Action(() =>
            {
                richTextBoxReceive.AppendText($"接收: {data}\n"); // 显示接收的数据
            }));
        }

        // 发送数据
        private void buttonSend_Click(object sender, EventArgs e)
        {
            if (serialPortSend.IsOpen)
            {
                string data = textBoxSend.Text;
                serialPortSend.WriteLine(data); // 发送数据
                richTextBoxReceive.AppendText($"发送: {data}\n"); // 显示发送的数据
            }
            else
            {
                MessageBox.Show("发送串口未打开！");
            }
        }

        // 初始化界面
        private void InitializeComponent()
        {
            this.textBoxSend = new System.Windows.Forms.TextBox();
            this.buttonSend = new System.Windows.Forms.Button();
            this.richTextBoxReceive = new System.Windows.Forms.RichTextBox();
            this.SuspendLayout();

            // textBoxSend (发送数据)
            this.textBoxSend.Location = new System.Drawing.Point(20, 20);
            this.textBoxSend.Name = "textBoxSend";
            this.textBoxSend.Size = new System.Drawing.Size(200, 23);
            this.textBoxSend.TabIndex = 0;

            // buttonSend (发送按钮)
            this.buttonSend.Location = new System.Drawing.Point(230, 20);
            this.buttonSend.Name = "buttonSend";
            this.buttonSend.Size = new System.Drawing.Size(75, 23);
            this.buttonSend.TabIndex = 1;
            this.buttonSend.Text = "发送";
            this.buttonSend.Click += new System.EventHandler(this.buttonSend_Click);

            // richTextBoxReceive (接收数据显示)
            this.richTextBoxReceive.Location = new System.Drawing.Point(20, 60);
            this.richTextBoxReceive.Name = "richTextBoxReceive";
            this.richTextBoxReceive.Size = new System.Drawing.Size(285, 150);
            this.richTextBoxReceive.TabIndex = 2;
            this.richTextBoxReceive.ReadOnly = true;

            // Form1
            this.ClientSize = new System.Drawing.Size(325, 230);
            this.Controls.Add(this.richTextBoxReceive);
            this.Controls.Add(this.buttonSend);
            this.Controls.Add(this.textBoxSend);
            this.Name = "Form1";
            this.Text = "串口通讯器-COM1";
            this.ResumeLayout(false);
            this.PerformLayout();
        }

        private TextBox textBoxSend;
        private Button buttonSend;
        private RichTextBox richTextBoxReceive;
    }
    
}
